<?php
define( '_JEXEC', 1 );
define('JPATH_BASE', substr(substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "administra")),0,-1));

//if (!isset($_SERVER["HTTP_REFERER"])) exit("Direct access not allowed.");
$mosConfig_absolute_path =substr(JPATH_BASE, 0, strpos(JPATH_BASE, "/administra")); 

define( 'DS', DIRECTORY_SEPARATOR );
$parts = explode(DS, JPATH_BASE);
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'methods.php');
require_once ( JPATH_BASE .DS.'configuration.php' );
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'base'.DS.'object.php');
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'database'.DS.'database.php');
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'database'.DS.'database'.DS.'mysql.php');
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'filesystem'.DS.'folder.php');

$config = new JConfig();
$options = array ("host" => $config->host, "user" => $config->user, "password" => $config->password, "database" => $config->db, "prefix" => $config->dbprefix);

class iJoomlaDatabase extends JDatabaseMySQL{
	public function __construct($options){
		parent::__construct($options);
	}
}

$database = new iJoomlaDatabase($options);


$task = JRequest::getVar("task", "");

switch($task){
	case "change_survey" : {		
		changeSurvey();
		break;
	}
	case "change_survey_skip" : {
		changeSurveySkip();
		break;
	}
	case "change_page_skip" : {
		changePageSkip();
		break;
	}
	case "change_question_skip" : {
		changeQuestionSkip();
		break;
	}
}

function changeQuestionSkip(){
	global $database;
	$question_id = JRequest::getVar("question_id", "");
	$sql = "select id, value from #__ijoomla_surveys_answers where question_id=".$question_id." order by id asc";
	$database->setQuery($sql);
	$database->query();
	$result = $database->loadAssocList();
	if(count($result)>0){
		$return = '<select size="8" multiple="multiple" id="answer_value[]" name="answer_value[]">';		
		foreach($result as $key=>$value){			
			$return .= "<option value=\"".$value["id"]."\">".$value["value"]."</option>";			
		}
		$return .= "</select>";
		echo $return;
	}
	else{
		echo "";
	}
}

function changePageSkip(){
	$div = JRequest::getVar("div", "");
	global $database;
	$page_id = JRequest::getVar("page_id", "");
	$survey_id = JRequest::getVar("survey_id", "");
	
	if($div == "question"){
		$sql = "select id, title from #__ijoomla_surveys_questions where page_id=".$page_id." and question_type not in(9, 11)";
		$database->setQuery($sql);
		$database->query();
		$result = $database->loadAssocList();
		if(count($result)>0){
			$return = '<select name="question_id" id="question_id" onchange="changeQuestionSkip(this, \''.$survey_id.'\');">';
			$return .= "<option value=\"0\">Please select...</option>";
			foreach($result as $key=>$value){			
				$return .= "<option value=\"".$value["id"]."\">".$value["title"]."</option>";			
			}
			$return .= "</select>";
			echo $return;
		}
		else{
			echo "<b style=\"color: red;\">This Page does not have any questions</b>";
		}
	}
	elseif($div == "page_target_skip"){
		$sql = "select id from #__ijoomla_surveys_pages where ordering=(select max(ordering) from #__ijoomla_surveys_pages where survey_id=".intval($survey_id).")";
		$database->setQuery($sql);
		$database->query();
		$last_page = $database->loadResult();
		
		if($last_page == $page_id){
			echo "<b style=\"color: red;\">You can`t add skip logics to the last page of the survey</b>";
			return;
		}
		
		$sql = "select id, title, ordering from #__ijoomla_surveys_pages where id<>".$page_id." and survey_id=".intval($survey_id);		
		$database->setQuery($sql);
		$database->query();
		$result = $database->loadAssocList();
		if(count($result)>0){
			$return = '<select name="page_target" id="page_target">';
			$return .= "<option value=\"0\">Please select...</option>";
			foreach($result as $key=>$value){
				$return .= "<option value=\"".$value["id"]."\">".$value["title"]."</option>";			
			}
			$return .= '<option value="-1">Survey end</option>';
			$return .= "</select>";
			echo $return;
		}		
	}
}

function changeSurveySkip(){
	global $database;
	$survey_id = JRequest::getVar("survey_id", "0");
	$sql = "select id, title from #__ijoomla_surveys_pages where survey_id=".intval($survey_id);
	$database->setQuery($sql);
	$database->query();
	$result = $database->loadAssocList();
	
	if(count($result)>0){
		$return = '<select name="page_id" id="page_id" onchange="changePageSkip(this, \''.$survey_id.'\');">';
		$return .= "<option value=\"0\">Please select...</option>";
		foreach($result as $key=>$value){			
			$return .= "<option value=\"".$value["id"]."\">".$value["title"]."</option>";			
		}
		$return .= "</select>";
		echo $return;
	}
	else{
		echo "<b style=\"color: red;\">Please create a page for this survey first</b>";
	}
}

function changeSurvey(){
	global $database;
	$survey_id = JRequest::getVar("survey", "0");
	$default = JRequest::getVar("page_id", "0");
	$sql = "select id, title from #__ijoomla_surveys_pages where survey_id=".intval($survey_id);
	$database->setQuery($sql);
	$database->query();
	$result = $database->loadAssocList();
	
	if(count($result)>0){
		$return = '<select name="page_id" id="page_id">';
		$selected = "";
		$return .= "<option value=\"0\">Please select...</option>";
		foreach($result as $key=>$value){
			$selected = "";
			if($value["id"] == $default){
				$selected = ' selected="selected" '; 
			}
			$return .= "<option value=\"".$value["id"]."\" ".$selected." >".$value["title"]."</option>";			
		}
		$return .= "</select>";
		echo $return;
	}
	else{
		echo "<b style=\"color: red;\">Please create a page for this survey first</b>";
	}	
}

?>